<?php 

	class Production extends MY_Controller {

		public function __construct() {
			parent::__construct();
			$this->load->model('Production_model', 'p');
			$this->load->model('Order_model', 'order');
			$this->_d['alias'] = 'production';
			// 产品分类
			$data = $this->c->getCateData('production',array('parentid'=>'0'));
			$this->_d['pcate'] = cate2list(0, $data);
			/*$this->load->model('Advertisement_model', 'ad');
			$this->_d['link'] = $this->ad->getChannelAds('link');
			// 案例分类
			$this->load->model('Article_model', 'article');
			$casecate = $this->c->L(array('alias'=>'case'),'cateid');
			foreach ($casecate as $k => $v) $casecateid[] = $v['cateid'];
			$this->_d['case'] = $this->article->L(array('cateid in ('.implode(',',$casecateid).')'=>''));*/
			
		}

		public function pcate($cateid=''){		
			$catedata1 = $this->c->O(array('cateid'=>$cateid));
			$_catedata2 = $this->c->L(array('parentid'=>$cateid),'*',0,0);
			if (!empty($_catedata2))
			{
				foreach ($_catedata2 as $k => $v){
					$catedata2[$v['cateid']] = $v;
					$cateid2[] = $v['cateid'];
				}
				$where = 'parentid in ('.implode(",",$cateid2).")";
			}
			
			$sql = 'select parentid,cateid,catename from '.$this->c->tbl.' where '.$where;
			$query = $this->db->query($sql);
			$_catedata3 = $query->result_array();
			$where = '';
			if (!empty($_catedata3))
			{
				foreach ($_catedata3 as $k => $v){
					$catedata3[$v['parentid']][$v['cateid']] = $v;
					$cateid3[] = $v['cateid'];
				}
				$where = 'a.cateid in ('.implode(",",$cateid3).")";
			}
			
			$sql = 'select a.imgthumb,a.cateid,b.parentid from '.$this->p->tbl.' as a left join '.$this->c->tbl.' as b on a.cateid = b.cateid where a.status = 1 AND '.$where;
			$query = $this->db->query($sql);
			$_pimgthumb = $query->result_array();
			if (!empty($_pimgthumb)){
				foreach ($_pimgthumb as $k => $v) {
					if (empty($pimgthumb[$v['parentid']])) { $pimgthumb[$v['parentid']] = $v['imgthumb'];}
				}
			}
			foreach ($catedata2 as $k => $v){
				if (!empty($catedata3[$v['cateid']])) {
					$catedata2[$v['cateid']]['subcate'] = $catedata3[$v['cateid']];
				}
				if (!empty($pimgthumb[$v['cateid']])) $catedata2[$v['cateid']]['imgthumb'] = $pimgthumb[$v['cateid']];
			}
			$this->_d['catename'] = @$catedata1['catename'];
			$this->_d['cateid']=$cateid;
			$this->_d['subcatedata'] = $catedata2;
			$this->load->view($this->_d['cfg']['tpl']. 'category', $this->_d); 
		}

		public function cate($cateid=''){
			$catedata = $this->c->O(array('cateid'=>$cateid));
			$subcatedata = $this->c->L(array('parentid'=>$cateid),'*',0,0);
			if (!empty($subcatedata))
			{
				foreach ($subcatedata as $k => $v) $subcateid[] = $v['cateid'];
				$where = 'cateid in ('.$cateid.','.implode(",",$subcateid).")";
			}
			else
			{
				$where = 'cateid = ' .$cateid;
			}
			$sql = 'select imgthumb,cateid from '.$this->p->tbl.' where status = 1 AND '.$where.' group by cateid';
			$query = $this->db->query($sql);
			$_pimgthumb = $query->result_array();
			if (!empty($_pimgthumb)){
				foreach ($_pimgthumb as $k => $v) $pimgthumb[$v['cateid']] = $v['imgthumb'];
			}
			foreach ($subcatedata as $k => $v){
				if (!empty($pimgthumb[$v['cateid']])) $subcatedata[$k]['imgthumb'] = $pimgthumb[$v['cateid']];
			}
			$this->_d['catename'] = @$catedata['catename'];
			$this->_d['cateid']=$cateid;
			$this->_d['subcatedata'] = $subcatedata;
			$this->load->view($this->_d['cfg']['tpl']. 'category', $this->_d); 
		}

		public function index($cateid='')
		{
			$sdata['status'] = 1;
			/*$pcatedata = $this->c->O(array('cateid'=>$parentid));
			$this->_d['pcatedata'] = $pcatedata;*/
			$catedata = $this->c->O(array('cateid'=>$cateid));
			$this->_d['catename'] = @$catedata['catename'];
			if(!empty($cateid))
				$sdata['cateid'] = $cateid;		
			$production = $this->p->L($sdata,'*',0, 0,'sort');
			$this->_d['production'] = $production;
			$this->_d['cateid']=$cateid;
			/*$subcatedata = $this->c->L(array('parentid'=>$parentid),'*',0,0);
			$this->_d['subcatedata'] = $subcatedata;*/
			$this->load->view($this->_d['cfg']['tpl']. 'production', $this->_d); 
		}

		public function detail($producid='')
		{
			$this->setCache();
			$row = $this->p->O(array('productid'=>$producid));
			$navcate = $this->c->getParents($row['cateid']);
			if (empty($row)) show_404();
			$row['attachment'] = $this->getAttach($producid,'production');
			$this->_d['row'] = $row;
			$this->_d['navcate'] = $navcate;
			$this->_d['related'] = $this->p->L(array('cateid'=>$row['cateid']));
			$this->load->view($this->_d['cfg']['tpl']. 'production_detail', $this->_d); 

		}

		public function index_detail($advertid='')
		{
			$query = $this->db->query('SELECT title FROM ey_advertising WHERE advertid = '.$advertid);
			$title = $query->result_array()[0]['title'];
			$row = $this->db->select('*')->where('name',$title)->get('ey_production')->row_array();
			$this->setCache();
			$navcate = $this->c->getParents($row['cateid']);
			if (empty($row)) show_404();
			$row['attachment'] = $this->getAttach($row['productid'],'production');
			$this->_d['row'] = $row;
			$this->_d['navcate'] = $navcate;
			$this->_d['related'] = $this->p->L(array('cateid'=>$row['cateid']));
			$this->load->view($this->_d['cfg']['tpl']. 'production_detail', $this->_d);

		}

		public function order($productid='',$en='')
		{
			if ($this->form_validation->run('order/add') == false)
			{
				if (validation_errors() != '')
				{
					$retmsg['code'] = '0';
					$retmsg['msg'] = html2text(validation_errors());
					exit(json_encode($retmsg));
				}
				if ($en != '') $this->_d['lg'] = $en;
				$row = $this->p->O(array('productid'=>$productid));
				$row['attachment'] = $this->getAttach($productid,'production');
				$this->_d['production'] = $row;
				$this->_d['catedata'] = $this->c->O(array('cateid'=>$row['cateid']));
				$this->load->view($this->_d['cfg']['tpl'] . 'order', $this->_d);
			}
			else
			{
				$postdata = $this->input->post();
				/*$vcode = $postdata['vcode'];
				if(strtolower($vcode)!=$this->session->userdata("vcode"))
				{
					$retmsg['code'] = '0';
					$retmsg['msg'] = '请输入正确的验证码';
					exit(json_encode($retmsg));
				}*/
				$postdata['ctime'] = time();
				$postdata['ip_address'] = $this->input->ip_address();
				$postdata['url'] = site_url('production/detail/'.$postdata['productid']);
				if (!empty($postdata['lg']) && $postdata['lg'] == 'en')
				{
					$postdata['url'] = site_url('en/production/detail'.$postdata['productid']);
				}
				if ($this->order->A($postdata))
				{
					$retmsg['code'] = '1';
					$retmsg['msg'] = "感谢您的询价,我们会及时给您回复";
					exit(json_encode($retmsg));
				}
				else
				{
					$retmsg['code'] = '0';
					$retmsg['msg'] = "递交失败，请重试";
					exit(json_encode($retmsg));
				}

			}
			
		}
	}